* This file generates the main results. 
********************************************************************************
************* Dependent variables **********************************************
********************************************************************************
gen Right = ((M + C + L + KD)/VV)*100
label variable Right "Right-wing bloc vote share"

gen Left = ((S+V+MP)/VV)*100
label variable Left "Left-wing bloc vote share"

* Dependent variable 
gen LWA = Left - Right
label variable LWA "Left-wing advantage"

* The Social democrats:
gen Share_S = (S/ VV) * 100
label variable Share_S "Vote share Social democratic party"

* The left party:
gen Share_V =( V / VV) * 100
label variable Share_V "Vote share Left party"

* The Green party:
gen Share_MP = (MP  / VV) * 100
label variable Share_MP "Vote share Green party"

* The Moderate party:
gen Share_M = (M  / VV) * 100
label variable Share_M "Vote share Moderate party"

* The Liberal party:
gen Share_L = (L / VV) * 100
label variable Share_L "Vote share Liberal party"

* The Center party:
gen Share_C = (C / VV) * 100
label variable Share_C "Vote share Center party"

* The Christian democrats:
gen Share_KD  =  (KD / VV) * 100
label variable Share_KD "Vote share Center party"


* Change in support for the government parties
gen inc = 0 
replace inc = d.Share_S if Year == 2006
replace inc = d.Right if Year > 2006 
label variable inc "Change in government support"

********************************************************************************
*************************** Independent vairables ******************************
********************************************************************************

* Share of non-employed:
gen NP = 1-employed
replace NP = NP * 100
label variable NP "Share of workers not employed"


* Gen growth in non-employment rate
gen GNE = d.NP/l.NP
replace GNE = GNE * 100
label variable GNE "Growth in the non-employment rate"

* Gen growth in unemployment
replace Unemployment = Unemployment * 100
gen GUE = (D.Unemployment / L.Unemployment) * 100
label variable GUE "Growth in unemployment"

* Shifts in stock based wealth:
gen Stock_shifts= ((Stock_growth * Stock1999) / Voters1999) / 100000
label variable Stock_shifts "Shifts in stock wealth per voter"

* Shifts in mutualfund based wealth: 
gen Mutualfund_shifts =  ((Mutualfund_growth  * Mutualfund1999 ) / Voters1999) / 100000
label variable Mutualfund_shifts "Mutual fund wealth per voter"

* Shifts in housebased based wealth: 
gen House_shifts =  ((House_growth  * House1999) / Voters1999) / 100000
label variable House_shifts "House wealth per voter"

* Shifts in apartment based wealth:
gen Apartment_shifts =  ((Apartment_growth  * Apartment1999) / Voters1999) / 100000
label variable Apartment_shifts "Apartment wealth per voter"
* The costs of these assets in the form of interest rates. Multiplied by 4 since it is 4 years between the elections.

* Generating total wealth shifts. 
gen Net_returns = (((Mutualfund_growth  * Mutualfund1999 )+(House_growth  * House1999) ///
+(Apartment_growth  * Apartment1999)-(4*(Short_rate*Debt1999))) / Voters1999) / 100000
label variable Net_returns "Net asset returns"

* Age squared:
gen Agesq = Age * Age

********************************************************************************
***************************** Figure 1 *****************************************
// This code generates the histogram of growth in non-employment on page 12 
hist GNE if GNE > -51 & GNE <51 & Year > 2002 & Year < 2018 , freq 

********************************************************************************
***************************** Figure 2 *****************************************
********************************************************************************

// This code generates the histogram of net-asset returns in non-employment on page 15 
hist Net_returns if Net_returns > -3 & Net_returns < 9 & Year > 2002 & Year < 2018 , freq 
********************************************************************************

********************************************************************************
******************** Table Main 1 on page 18 ***********************************
********************************************************************************
eststo clear 
* Column 1: Changes in left-wing advantage and growth in unemployment:
qui regress LWA NP if Year > 1999 ,  vce(cluster Id)
estimates store LWA_OLS1

* Column 2 Multivariate regression unemployment in first diffrence:
qui reghdfe d.LWA GNE if Year > 2002, absorb(i.Mid##i.Year Id) vce(cluster Id)
estadd local year "Yes",replace
estimates store LWA_OLS2

* Column 3 Multivariate regression relative growth rate in unemployment:
qui reghdfe d.LWA GNE d.(Migrant Education Age Agesq) if Year > 2002, absorb(i.Mid##i.Year Id) vce(cluster Id)
estimates store LWA_OLS3

* Column 4: Multivariate regression relative growth rate in unemployment:
qui reghdfe  d.LWA c.GNE##c.Net_returns d.(Migrant Education Age Agesq) cd.(Migrant Education Age Agesq )#c.Net_returns if Year > 2002 ,absorb(i.Mid##i.Year i.Id) vce(cluster Id)
estimates store LWA_OLS4
esttab LWA_OLS1 LWA_OLS2 LWA_OLS3 LWA_OLS4 , se  keep(NP GNE Net_returns c.GNE#c.Net_returns _cons ) 

********************************************************************************
*************************** Figure 3 on page 19 ********************************
********************************************************************************
qui margins, dydx(GNE) at(Net_returns=(-1(1)10))
marginsplot, ciopts(lcolor(green)) graphregion(margin(zero )) addplot(hist Net_returns if Net_returns < 11 & Year > 2002,  freq yaxis(2) width(1) legend(off) fcolor(none) color(black) lwidth(thick) graphregion(margin(zero ))) 

********************************************************************************
************************** Figure 4 on page 21 *********************************
********************************************************************************
qui reghdfe d.Share_S c.GNE##c.Net_returns cd(Migrant Education Age Agesq)##c.Net_returns if Year > 2002 ,absorb(i.Mid##i.Year Id) vce(cluster Id)
qui margins, dydx(GNE) at(Net_returns=(-1(1)10))  level(95)  saving(Socialdemocrats)

qui reghdfe d.Share_V c.GNE##c.Net_returns cd(Migrant Education Age Agesq)##c.Net_returns if Year > 2002 ,absorb(i.Mid##i.Year Id) vce(cluster Id)
qui margins, dydx(GNE) at(Net_returns=(-1(1)10))  level(95)  saving(Leftparty)

qui reghdfe d.Share_MP c.GNE##c.Net_returns cd(Migrant Education Age Agesq)##c.Net_returns if Year > 2002 ,absorb(i.Mid##i.Year Id) vce(cluster Id)
qui margins, dydx(GNE) at(Net_returns=(-1(1)10))  level(95)  saving(Greenparty)  

combomarginsplot Socialdemocrats Leftparty Greenparty, ///
    labels("Social Democratic Party" "Left Party" "Green Party" ) yline(0) 

********************************************************************************	
****************************** Table 2 Appendix B ******************************
********************************************************************************	
eststo clear
local vars SWPV1999 HWPV1999 MWPV1999 AWPV1999 DPV1999 Wealth1999 Voters LWA  Left Migrant Education NP Age  
estpost summarize `vars' if Year > 1999 & Year < 2018
esttab using summarystats.tex, cells("mean(fmt(2)) sd(fmt(2)) min(fmt(2)) max(fmt(2)) count(fmt(0))") noobs nonumber label title("Summary statistics") replace

********************************************************************************
******************************** Table 4 Appendix C ****************************
********************************************************************************
// This code generates the first-stage estimates found on page 35 
eststo clear
* Column 1 House wealth ~ House proxy, initial value& year effects:  Net_returns
eststo: qui reghdfe d.Value_housePV House_shifts d.(NP Migrant Education Age Agesq)  ,absorb(i.Year) vce(cluster Id)
estimates store FS1

* Column 2 Apartment wealth ~ Apartment proxy, initial value& year effects:  
eststo: qui reghdfe d.Value_apartmentPV Apartment_shifts d.(NP Migrant Education Age Agesq)   ,absorb(i.Year) vce(cluster Id)
estimates store FS2

* Column 3 Mutalfunds wealth ~ Mutalfunds proxy, initial value& year effects:
eststo: qui reghdfe d.Value_mutualfundPV Mutualfund_shifts d.(NP Migrant Education Age Agesq)   ,absorb(i.Year) vce(cluster Id)
estimates store FS3

* Column 4 Stock wealth ~ Stock wealth proxy: 
eststo: qui reghdfe d.Value_stockPV Stock_shifts d.(NP Migrant Education Age Agesq)   ,absorb(i.Year) vce(cluster Id)
estimates store FS4

* Column 5 Stock wealth ~ Stock wealth proxy: 
eststo: qui reghdfe d.Net_wealth Net_returns d.(NP Migrant Education Age Agesq)   ,absorb(i.Year) vce(cluster Id)
estimates store FS5
esttab FS1 FS2 FS3 FS4 FS5 , keep(House_shifts Apartment_shifts Mutualfund_shifts Stock_shifts Net_returns  _cons )

********************************************************************************
****************** Figure 5 and 6 in Appendix D ********************************
********************************************************************************
	
// Figure 5 page 36
qui reghdfe d.Share_M c.GNE##c.Net_returns cd(Migrant Education Age Agesq)##c.Net_returns  if Year > 2002  ,absorb(i.Mid##i.Year i.Id) vce(cluster Id)
qui margins, dydx(GNE) at(Net_returns=(-1(1)10))  level(95)  saving(Moderates)

qui reghdfe d.Share_C c.GNE##c.Net_returns cd(Migrant Education Age Agesq)##c.Net_returns  if Year > 2002 ,absorb(i.Mid##i.Year i.Id) vce(cluster Id)
qui margins, dydx(GNE) at(Net_returns=(-1(1)10))  level(95)  saving(Centerparty)
combomarginsplot Moderates1 Centerparty1  , ///
    labels("Moderate Party" "Center Party") yline(0) 

// Figure 6 page 37
qui reghdfe d.Share_L c.GNE##c.Net_returns cd(Migrant Education Age Agesq)##c.Net_returns  if Year > 2002 ,absorb(i.Mid##i.Year i.Id) vce(cluster Id)
qui margins, dydx(GNE) at(Net_returns=(-1(1)10))  level(95)  saving(Liberals)

qui reghdfe d.Share_KD c.GNE##c.Net_returns cd(Migrant Education Age Agesq)##c.Net_returns if Year > 2002 ,absorb(i.Mid##i.Year i.Id) vce(cluster Id)
qui margins, dydx(GNE) at(Net_returns=(-1(1)10))  level(95)  saving(Christiandemocrats)

combomarginsplot Liberals Christiandemocrats , ///
    labels("Liberal Party" "Christian Democrats") yline(0) 
	
********************************************************************************
************************* Figure 7 Appendix H **********************************
********************************************************************************	
	
// Figure 7 page 38
xtile Quantiles = Net_returns, nquantiles(5) 

* Generating bins
forval i = 1/5 {
gen bin`i' = 0
replace bin`i' =1 if Quantiles == `i'
}
* Generate bin medians as evaluations points:
* Bin1
sum Net_returns if bin1 ==1, detail 
gen Net_returns1 = Net_returns - (0.063)  
* Bin2
sum Net_returns if bin2 ==1 , detail
gen Net_returns2 = Net_returns - .23  
* Bin3
sum Net_returns if bin3 ==1 , detail
gen Net_returns3 = Net_returns - .43  
* Bin4
sum Net_returns if bin4 ==1 , detail
gen Net_returns4 = Net_returns - .89   
* Bin5
sum Net_returns if bin5 ==1 , detail
gen Net_returns5 = Net_returns - 1.83

* Estimating a bin-model:
reghdfe d.LWA bin1-bin5 c.GNE#c.(bin1-bin5) ///
c.Net_returns1#c.bin1 c.Net_returns2#c.bin2 c.Net_returns3#c.bin3 c.Net_returns4#c.bin4 c.Net_returns5#c.bin5 ///
c.Net_returns1#c.bin1#c.GNE c.Net_returns2#c.bin2#c.GNE c.Net_returns3#c.bin3#c.GNE c.Net_returns4#c.bin4#c.GNE c.Net_returns5#c.bin5#c.GNE ///
cd.(Age Education Migrant Agesq)##c.bin1-bin5 if Year > 2002 ,absorb(i.Mid##i.Year i.Id) vce(cluster Id)
coefplot , vertical keep( c.GNE#c.bin1 c.GNE#c.bin2 c.GNE#c.bin3 c.GNE#c.bin4 c.GNE#c.bin5 ) yline(0) ///
coeflabel(c.GNE#c.bin1 = " 0.06" c.GNE#c.bin2 = "0.25" c.GNE#c.bin3 = "0.45" c.GNE#c.bin4 = ".90" c.GNE#c.bin5 = "1.80" ) ///
xtitle("Wealth") ytitle("Marginal effect non-employment")
 
******************************************************************************** 
************************* Figure 8 Apendix F ***********************************
********************************************************************************
qui reghdfe inc c.GNE##c.Net_returns cd.(Migrant Education Age Agesq)##c.Net_returns if Year > 2002 ,absorb(i.Mid##i.Year i.Id i.Id) vce(cluster Id)

qui margins, dydx(GNE) at(Net_returns=(-1(1)10))
marginsplot, ciopts(lcolor(green)) graphregion(margin(zero )) addplot(hist Net_returns if Net_returns < 11 ,  freq yaxis(2) width(1) legend(off) fcolor(none) color(black) lwidth(thick) graphregion(margin(zero ))) 

********************************************************************************
************************** Table 7 and 8 in Appendix I *************************
********************************************************************************
* Table 7
eststo clear
set matsize 10000
qui: regress  d.LWA c.GNE##c.Net_returns d.(Migrant Education Age Agesq  ) cd.(Migrant Education Age Agesq)#c.Net_returns  i.Mid##i.Year if Year < 2018 & Year > 2002
estimates store full_`num'
qui: regress  d.LWA c.GNE##c.Net_returns d.(Migrant Education Age Agesq  ) cd.(Migrant Education Age Agesq)#c.Net_returns  i.Mid if Year == 2006 
estimates store y2006_`num'
qui: regress  d.LWA c.GNE##c.Net_returns d.(Migrant Education Age Agesq  ) cd.(Migrant Education Age Agesq)#c.Net_returns  i.Mid if Year == 2010 
estimates store y2010_`num'
qui: regress  d.LWA c.GNE##c.Net_returns d.(Migrant Education Age Agesq  ) cd.(Migrant Education Age Agesq)#c.Net_returns  i.Mid if Year == 2014 
estimates store y2014_`num'
esttab full_`num' y2006_`num' y2010_`num' y2014_`num' , keep( GNE Net_returns c.GNE#c.Net_returns _cons) 


// Table 8 on page 45
 qui: suest  y2006_`num' y2010_`num' y2014_`num', vce (cluster Id)

 // Test 2006 & 2010,
 test  [y2006_`num'_mean]GNE#c.Net_returns=[y2010_`num'_mean]c.GNE#c.Net_returns
 
 // Test 2006 & 2014
 test  [y2006_`num'_mean]GNE#c.Net_returns=[y2014_`num'_mean]c.GNE#c.Net_returns
 
 // Test 2010 & 2014 
 test  [y2010_`num'_mean]GNE#c.Net_returns=[y2014_`num'_mean]c.GNE#c.Net_returns
 
********************************************************************************
******************************* Table 9 Appendix J *****************************
********************************************************************************


*** [Table Main 1 : Regression on changes in the support for the left-wing advantage]****
eststo clear 
* Column 1: Changes in left-wing advantage and growth in unemployment:
qui regress LWA Unemployment if Year > 1999  ,  vce(cluster Id)
estimates store LWA_GUE1

* Column 2 Multivariate regression unemployment in first diffrence:
qui reghdfe d.LWA GUE if Year > 2002   , absorb(i.Mid##i.Year Id) vce(cluster Id)
estadd local year "Yes",replace
estimates store LWA_GUE2

* Column 3 Multivariate regression relative growth rate in unemployment:
qui reghdfe d.LWA GUE d.(Migrant Education Age Agesq) if Year > 2002,  absorb(i.Mid##i.Year Id) vce(cluster Id)
estadd local year "Yes", replace
estimates store LWA_GUE3

* Column 4: Multivariate regression relative growth rate in unemployment:
qui reghdfe  d.LWA c.GUE##c.Net_returns d.(Migrant Education Age Agesq) cd.(Migrant Education Age Agesq)#c.Net_returns if Year > 2002 ,absorb(i.Mid##i.Year i.Id) vce(cluster Id)
estimates store LWA_GUE4

esttab LWA_GUE1 LWA_GUE2 LWA_GUE3 LWA_GUE4, keep(Unemployment GUE Net_returns c.GUE#c.Net_returns _cons) 

 





 

 
 